package com.woniuxy.carlife.carlifenewcomplaintsweb.controller;


import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.carlifecommonsweb.util.JwtUtil;
import com.woniuxy.carlife.carlifenewcomplaintsdomain.dto.UserMessageDto;
import com.woniuxy.carlife.carlifenewcomplaintsdomain.param.UserMessageParam;
import com.woniuxy.carlife.carlifenewcomplaintsweb.service.UserMessageService;
import com.woniuxy.carlife.carlifenewcomplaintsweb.utils.StatusMessageEnum;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author yp
 * @ClassName:UserMessageController
 * @Description:用户消息
 * @date 2021/4/7 2:32
 **/
@RestController
@Slf4j
@Api(tags = "用户消息管理接口")
@RequestMapping("/userMessage")
public class UserMessageController {

    @Resource
    private UserMessageService userMessageService;

    /*
     * 功能描述〈添加用户消息〉
     * @author:yp
     * @param:UserMessageParam
     * @return:
     * @Date 2021/4/12 9:58
     * @throws
     */
    @PostMapping("/platformmanage/addUserMessage")
    @ApiOperation(value = "添加用户消息信息",notes = "<span style='color:red;'>添加用户消息信息的接口</span>")
    @ApiResponses({
            @ApiResponse(code = 20000,message = "添加成功"),
            @ApiResponse(code=20001,message = "添加失败")
    })
    public Result addUserMessage(@RequestBody @ApiParam UserMessageParam userMessageParam){
        if(userMessageParam==null){
            return new Result<>(false,StatusCode.ERROR,"参数不能为空");
        }
        ServletRequestAttributes sra =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
        userMessageParam.setUserId(tokenUserId);
        log.info("开始业务:添加用户消息");
        int num = userMessageService.addUserMessage(userMessageParam);
        log.info("结束业务:添加用户消息");
        if (num==1){
            return new Result(true, StatusCode.OK, StatusMessageEnum.SAVE_SUCCESS.toString());
        }
        return new Result(false,StatusCode.ERROR,StatusMessageEnum.SAVE_FAIL.toString());
    }

    /*
     * 功能描述〈查询用户消息已读和未读〉
     * @author:yp
     * @param:UserMessageParam
     * @return:List<UserMessageDto>
     * @Date 2021/4/12 10:22
     * @throws
     */
    @GetMapping("/ordinary/findAllUserMessage")
    @ApiOperation(value = "查询已读和未读用户消息信息",notes = "<span style='color:red;'>查询用户消息已读和未读的接口</span>")
    @ApiResponses({
            @ApiResponse(code = 20000,message = "查询成功"),
            @ApiResponse(code=20001,message = "查询失败")
    })
    public Result findAllUserMessage(){
        ServletRequestAttributes sra =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
        log.info("开始业务:查询用户消息");
        List<UserMessageDto> list = userMessageService.findAllUserMessage(tokenUserId);
        log.info("结束业务:查询用户消息");
        if (list.size()!=0){
            return new Result(true,StatusCode.OK,StatusMessageEnum.FIND_SUCCESS.toString(),list);
        }
        return new Result(false,StatusCode.ERROR,StatusMessageEnum.FIND_FAIL.toString());
    }

    /*
     * 功能描述〈修改消息状态〉
     * @author:yp
     * @param: UserMessageParam
     * @return:
     * @Date 2021/4/12 11:29
     * @throws
     */
    @GetMapping("/ordinary/updateUserMessage")
    @ApiOperation(value = "修改用户消息状态信息",notes = "<span style='color:red;'>修改用户消息状态信息的接口</span>")
    @ApiResponses({
            @ApiResponse(code = 20000,message = "修改成功"),
            @ApiResponse(code=20001,message = "修改失败")
    })
    @ApiImplicitParams({
            //dataType:参数类型
            //paramType:参数由哪里获取     path->从路径中获取，query->?传参，body->ajax请求
            @ApiImplicitParam(name = "userMessageId",value = "用户消息id",dataType = "int",paramType = "query",example = "1"),
    })
    public Result updateUserMessage(Integer userMessageId){
        if(userMessageId==null){
            return new Result(false,StatusCode.ERROR,"参数错误");
        }
        log.info("开始业务:修改用户消息状态");
        int num = userMessageService.updateUserMessage(userMessageId);
        log.info("结束业务:修改用户消息状态");
        if (num==1){
            return new Result(true,StatusCode.OK,StatusMessageEnum.UPDATE_SUCCESS.toString());
        }
        return new Result(false,StatusCode.ERROR,StatusMessageEnum.UPDATE_FAIL.toString());
    }

    /*
     * 功能描述〈删除消息〉
     * @author:yp
     * @param:UserMessageParam
     * @return:
     * @Date 2021/4/12 10:39
     * @throws
     */
    @GetMapping("/ordinary/deleteUserMessage")
    @ApiOperation(value = "删除消息信息",notes = "<span style='color:red;'>删除消息信息的接口</span>")
    @ApiResponses({
            @ApiResponse(code = 20000,message = "删除成功"),
            @ApiResponse(code=20001,message = "删除失败")
    })
    @ApiImplicitParams({
            //dataType:参数类型
            //paramType:参数由哪里获取     path->从路径中获取，query->?传参，body->ajax请求
            @ApiImplicitParam(name = "userMessageId",value = "用户消息id",dataType = "int",paramType = "query",example = "1"),
    })
    public Result deleteUserMessage(Integer userMessageId) {
        if (userMessageId == null) {
            return new Result(false, StatusCode.ERROR, "参数错误");
        }
        log.info("开始业务:删除用户消息");
        int num = userMessageService.deleteUserMessage(userMessageId);
        log.info("结束业务:删除用户消息");
        if (num == 1) {
            return new Result(true, StatusCode.OK, StatusMessageEnum.UPDATE_SUCCESS.toString());
        }
        return new Result(false, StatusCode.ERROR, StatusMessageEnum.UPDATE_FAIL.toString());
    }
}

